//
//  MineHeadView.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/2/23.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "MineHeadView.h"
#import "LoginViewController.h"
#import "SettingViewController.h"
#import "MyInforViewController.h"
#import "AppContext.h"
#import "AccountModel.h"
#import "SDWebImageManager.h"

@implementation MineHeadView
-(void)awakeFromNib{
    self.headBt.layer.cornerRadius=43;
    self.headBt.clipsToBounds=YES;
    self.headBt.layer.borderWidth=2;
    self.headBt.layer.borderColor=[UIColor colorWithHexString:@"000000"].CGColor;
    self.loginBt.layer.cornerRadius=18;
    self.loginBt.clipsToBounds=YES;
    [self.bgImageView addSubview:self.nameLabel];
    [self.bgImageView addSubview:self.sexImageView];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (IBAction)headImageAction:(id)sender {
    UIViewController *vc=[UIView viewController:self];

    //我的资料
    if (!APP_CONTEXT.account) {
        [LoginViewController presentLoginViewController:vc withCompleteBlock:nil];
        return;
    }

    MyInforViewController *myVC=[[MyInforViewController alloc] init];
    [vc.navigationController pushViewController:myVC animated:YES];
    
}

- (IBAction)loginAction:(id)sender {
    UIViewController *vc=[UIView viewController:self];
    if(!APP_CONTEXT.account) {
        [LoginViewController presentLoginViewController:vc withCompleteBlock:nil];
        return;
    }
}

- (IBAction)setAction:(id)sender {
      UIViewController *vc=[UIView viewController:self];
    if (!APP_CONTEXT.account) {
        [LoginViewController presentLoginViewController:vc withCompleteBlock:nil];
        return;
    }
    SettingViewController *setVC=[[SettingViewController alloc] init];
    [vc.navigationController pushViewController:setVC animated:YES];
}

- (void)refreshHeadView {
    if (APP_CONTEXT.account) {
        self.loginBt.hidden = YES;
        self.nameLabel.hidden=NO;
        self.sexImageView.hidden=NO;
        if (APP_CONTEXT.account.data.avatar) {
            self.nameLabel.top=self.headBt.bottom+6;
            self.sexImageView.top=self.headBt.bottom+8;
            self.nameLabel.text=APP_CONTEXT.account.data.firstname;
            [self.nameLabel sizeToFit];
            self.nameLabel.center=CGPointMake(self.width/2, self.nameLabel.center.y);
            self.sexImageView.left=self.nameLabel.right+3;
            switch (APP_CONTEXT.account.data.gender) {
                case 0:
                    self.sexImageView.image=[UIImage imageNamed:@"icon_girl"];
                    break;
                case 1:
                   self.sexImageView.image=[UIImage imageNamed:@"icon_boy"];
                    break;
                    
                default:
                    break;
            }
            NSString *urlStr = APP_CONTEXT.account.data.avatar;
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:urlStr] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                [self.headBt setBackgroundImage:image forState:UIControlStateNormal];
            }];
        }
    } else {
        self.loginBt.hidden = NO;
        self.nameLabel.hidden=YES;
        self.sexImageView.hidden=YES;
        [self.headBt setBackgroundImage:[UIImage imageNamed:@"pic_head_moren"] forState:UIControlStateNormal];
    }
}
@end
